package com.yqwh.coder.jpush;

import java.io.InputStreamReader;
import java.util.Properties;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送工具类：实际应用实践
 * 
 * @author hejie
 * @date 2018年3月21日
 */
public class JpushUtil {
	private static JPushClient jpushClient;
	private static ClientConfig config;
	private static Properties props;
	private static final String appKey;
	private static final String masterSecret;

	static {
		config = ClientConfig.getInstance();
		config.setApnsProduction(true); // ios的坑 true为生产环境 false为开发环境
		config.setTimeToLive(60 * 60 * 24); // one day
		String fileName = "META-INF/base.properties";
		props = new Properties();
		try {
			props.load(new InputStreamReader(JpushUtil.class.getClassLoader().getResourceAsStream(fileName)));
			appKey = props.getProperty("jpush.appKey");
			masterSecret = props.getProperty("jpush.masterSecret");
			jpushClient = new JPushClient(masterSecret, appKey, null, config);
		} catch (Exception e) {
			throw new RuntimeException("base.properties找不到：" + e.getMessage());
		}
	}
	
	public static void closeJpushClient(){
		jpushClient.close();
	}
	
	/**
	 * ios通知(类型是：标签)(IosAlert支持标题)
	 * @return
	 */
	public static PushResult buildPushIosTagNotification(final PushMessage pushMessage) throws Exception{
         IosAlert iosAlert=new IosAlert.Builder().setTitleAndBody(pushMessage.getTitle(), pushMessage.getTitle(), pushMessage.getContext()).build();
         final PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.ios()).setAudience(Audience.tag_and(pushMessage.getTag())).setNotification(
 	    		new Notification.Builder().setAlert(iosAlert).build()).build();
         PushResult pushResult=jpushClient.sendPush(payload);
		 return pushResult;
    }
	/**
	 * 支持android自定义消息，设置标签tag和别名alias发送到指定客户端
	 * @return
	 */
	public static PushResult buildPushAndroidTagMessage(final PushMessage pushMessage) throws Exception{
		 final PushPayload payload =PushPayload.newBuilder()
				.setPlatform(Platform.android())
				.setAudience(Audience.newBuilder()
						.addAudienceTarget(AudienceTarget.tag(pushMessage.getTag()))
						/*一台设备只能设置一个别名且别名会覆盖*/
						// .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
						.build())
						.setMessage(Message.newBuilder()
								.setTitle(pushMessage.getTitle())
								.setMsgContent(pushMessage.getContext())
								//.addExtra("from", "JPush")
								.build())
								.build();
		 PushResult pushResult=jpushClient.sendPush(payload);
		 return pushResult;
	}
	 /**
     * android客户端广播通知(类型是：广播) 
     * @return
	 * @throws APIRequestException 
	 * @throws Exception 
     */
	public static PushResult buildPushAndroidBroadcastNotification(final PushMessage pushMessage) throws Exception{
		final PushPayload payload =PushPayload.newBuilder()
         .setPlatform(Platform.android())
         .setAudience(Audience.all())
         .setNotification(Notification.android(pushMessage.getContext(), pushMessage.getTitle(), null))
         .setMessage(Message.content(pushMessage.getContext()))
         .build();
		 PushResult pushResult=jpushClient.sendPush(payload);
		 return pushResult;
    }
	
	/**
	 *ios客户端广播通知(类型是：广播) 
	 * @return
	 */
	public static PushResult buildPushIosBroadcastNotification(final PushMessage pushMessage) throws Exception{
		final PushPayload payload =PushPayload.newBuilder()
				.setPlatform(Platform.ios())
				.setAudience(Audience.all())
				.setNotification(Notification.ios(pushMessage.getContext(),null))
				.setMessage(Message.newBuilder().setTitle(pushMessage.getTitle())
			    .setMsgContent(pushMessage.getContext()).build())
				.build();
		 PushResult pushResult=jpushClient.sendPush(payload);
		 return pushResult;
	}
	
	/**
	 * android和ios客戶端广播
	 * @return
	 */
	public static PushResult buildPushAndroidIosBroadcastNotification(final PushMessage pushMessage) throws Exception{
	  final PushPayload payload =PushPayload.newBuilder()
          .setPlatform(Platform.android_ios())//设置接受的平台
          .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
          .setNotification(Notification.alert(pushMessage.getContext()))
          .setMessage(Message.newBuilder().setTitle(pushMessage.getTitle())
  		  .setMsgContent(pushMessage.getContext()).build())
          .build();
	     PushResult pushResult=jpushClient.sendPush(payload);
		 return pushResult;
}
}
